//
//  GPSManager.m
//  YanXiuWang
//
//  Created by 慧趣小歪 on 17/1/7.
//  Copyright © 2017年 小分队. All rights reserved.
//

#import "GPSManager.h"

@interface GPSManager () {
    CLLocationManager *_locationManager;
    NSMutableArray *_callbacks;
}
@property(nonatomic, strong)NSMutableDictionary *blockMap;
@end

@implementation GPSManager

#pragma mark - lazy
- (NSMutableDictionary *)blockMap{
    if (_blockMap == nil) {
        _blockMap = [NSMutableDictionary dictionary];
    }
    return _blockMap;
    
}

- (void)getLocationOnComplete:(GPS_MANAGER_COMPLETE)onComplete {
    if (onComplete == nil) return;
    if (_callbacks == nil) {
        _callbacks = [NSMutableArray arrayWithCapacity:1];
    }
    
    [_callbacks addObject:[onComplete copy]];
    
    if (_locationManager == nil) {
        // 初始化定位管理器
        _locationManager = [[CLLocationManager alloc] init];
        // 设置代理
        _locationManager.delegate = self;
        // 设置定位精确度到米
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 设置过滤器为无
        _locationManager.distanceFilter = 1000;
        // 开始定位
        [_locationManager requestWhenInUseAuthorization];
        
        [_locationManager startUpdatingLocation];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    }
}


- (GPS_MANAGER_FAILED)onFailed{
    __weak typeof(self) weakSelf = self;
    return ^(OnFailure failureBlock) {
        weakSelf.blockMap[@"failed"] = failureBlock;//[failureBlock copy];
        return weakSelf;
    };

}


#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    for (GPS_MANAGER_COMPLETE callback in _callbacks) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        callback(locations.firstObject);
    }
    _callbacks = nil;
    [_locationManager stopUpdatingLocation];
    _locationManager = nil;
}

//- (void)locationManager:(CLLocationManager *)manager
//       didFailWithError:(NSError *)error{
//    for (GPS_MANAGER_COMPLETE callback in _callbacks) {
//        callback(nil);
//    }
//    _callbacks = nil;
//    [_locationManager stopUpdatingLocation];
//    _locationManager = nil;
//}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    OnFailure failed = self.blockMap[@"failed"];
    
    !failed ?: failed(error);
    
//    if ([error code] == kCLErrorDenied) {
//        [Toast.makeText(@"已拒绝使用定位系统") showOnDismiss:nil];
//    }
//    if ([error code] == kCLErrorLocationUnknown) {
//        [Toast.makeText(@"无法获取位置信息") showOnDismiss:nil];
//    }
}


#pragma mark - 判断是否开启了定位设置
- (BOOL)isCanLoating{
    //请开启定位服务
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status)
    {
        return NO;
    }else//开启的
    {
        //需要删除本地字符
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults removeObjectForKey:@"isPositioning"];
        [userDefaults synchronize];
        return YES;
    }

}

@end

#pragma mark - 单例相关
static dispatch_once_t onceToken;
static GPSManager *instance;
@implementation GPSManager (Once)

+ (nonnull instancetype)shared {
    return [GPSManager new];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    dispatch_once(&onceToken, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
}

- (instancetype)init {
    dispatch_once(&onceToken, ^{
        instance = [super init];
    });
    self = instance;
    return self;
}

- (id)copy {
    return self;
}

- (id)mutableCopy {
    return self;
}

@end
